package com.qsl.ssyx;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 线程池-计算完成时回调方法—complete
 * whenComplete：是执行当前任务的线程执行继续执行 whenComplete 的任务。
 * whenCompleteAsync：是执行把 whenCompleteAsync 这个任务继续提交给线程池来进行执行。
 * ======》 本次为whenComplete,   whenCompleteAsync省略。。。
 */
public class CompletableFutureDemo3 {
    public static void main(String[] args) {
        // 创建线程-线程池
        ExecutorService executorService = Executors.newFixedThreadPool(3);

        System.out.println(" main begin...");
        CompletableFuture<Integer> future = CompletableFuture.supplyAsync(() -> {
            System.out.println("当前线程：" + Thread.currentThread().getName());
//            int value = 1024;
            int value = 1024 / 0;  // 手动模拟异常
            System.out.println("value: " + value);
            return value;
        }, executorService).whenComplete((rs, exception) -> { // 返回结果，异常信息
            System.out.println("whenComplete: " + rs);
            System.out.println("exception: " + exception);
        });
        System.out.println(" main over ...");

    }
}
